import Vue from 'vue'
import VueRouter from 'vue-router'
import storageUtil from '../utils/storageUtil'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'admin',
    component: () => import('../views/Admin/Admin'),
    redirect:"/home",
    children:[
      {
        path: 'home',
        name: 'home',
        component: () => import('../views/Home/Home')
      },
      {
        path: 'user',
        name: 'user',
        component: () => import('../views/User/User'),
        meta:{desc:"用户管理"}
      },
      {
        path: 'role',
        name: 'role',
        component: () => import('../views/Role/Role'),
        meta:{desc:"角色管理"}
      },
      {
        path: 'charts',
        name: 'charts',
        component: () => import('../views/Charts/Charts'),
        meta:{desc:"数据可视化"}
      },
      {
        path: 'order',
        name: 'order',
        component: () => import('../views/Order/Order'),
        meta:{desc:"订单管理管理"}
      },
      {
        path: 'category',
        name: 'category',
        component: () => import('../views/Category/Category'),
        meta:{desc:"品类管理"}
      },
      {
        path: 'product',
        component: () => import('../views/Product/Product'),
        
        children:[
          {
            path:'',
            name:'productHome',
            component: () => import('../views/Product/ProductHome/ProductHome'),
            meta:{desc:"商品管理"},
          },{
            path:'detail',
            name:'productDetail',
            component: () => import('../views/Product/ProductDetail/ProductDetail'),
          },{
            path:'addUpdate',
            name:'productAddupdate',
            component: () => import('../views/Product/ProductAddUpdate/ProductAddUpdate'),
          },
        ]
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login/Login')
  },
  {
    path:'*',//用于匹配404页面
    name:'Error',
    component:() => import('../views/Error/Error')
}  

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if(to.path!=='/login'){
    var user = storageUtil.getUser()
    if(user._id === undefined){
      
      next('/login')
    }
    next()
  }
  next()
})

export default router
